/***************************************************************************
 *   Copyright (C) 2009 by Marian Skvarek   *
 *   skvarek.marian@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "CBarView.h"
#include "CBar.h"
#include "CScore.h"
#include "CSymbol.h"
#include "CSymbolView.h"
#include "CGlobals.h"
#include "CDebug.h"

#include <QGraphicsLineItem>

using namespace easyscore::globals;
using namespace easyscore::utils;

namespace easyscore
{
    namespace core
    {

        //Constructors, Destructors & Initialization
        CBarView::CBarView(CBar *model)
        {
            mModel = model;
            mSuspended = 0;
            mBarSeparatorLine = 0;
            mRemoveBarControl = 0;
            setAcceptHoverEvents(true);
            setZValue(4);
            showBorder(false);
        }

        void CBarView::initialize()
        {
            setRect(rect().x(),rect().y(),calculateBarWidth(),CGlobals::getBarHeight());

            mRemoveBarControl = new CRemoveBarControl();
            mRemoveBarControl->initialize();
            mRemoveBarControl->setPos( rect().width() - mRemoveBarControl->boundingRect().width(), 0 );
            mRemoveBarControl->setVisible(false);
            mRemoveBarControl->setParentBarView(this);

            mBarSeparatorLine = new QGraphicsLineItem();
            mBarSeparatorLine->setLine(0,0,0,4*CGlobals::getUnitSymbolWidth());
            mBarSeparatorLine->setParentItem(this);

            update();
        }

        void CBarView::clear()
        {
            if(mBarSeparatorLine)
            {
                delete mBarSeparatorLine;
                mBarSeparatorLine = 0;
            }

            if(mRemoveBarControl)
            {
                delete mRemoveBarControl;
                mRemoveBarControl = 0;
            }
        }


        CBarView::~CBarView()
        {
            clear();
        }

        //Overrides
        void CBarView::hoverEnterEvent ( QGraphicsSceneHoverEvent * )
        {
            setBrush(QColor(90,30,30,60));

            mRemoveBarControl->setVisible(true);

            mModel->splitRests();
        }

        void CBarView::hoverLeaveEvent ( QGraphicsSceneHoverEvent * )
        {
            setBrush(QColor(0,0,0,0));
            mRemoveBarControl->setVisible(false);

            mModel->removeTemporaryNoteGroup();
            mModel->unionRests();
        }

        //Public methods
        void CBarView::update()
        {
            if(mSuspended)
                return;

            int currentLeft = CGlobals::getBarLeftPadding();
            foreach ( CSymbol *symbol, mModel->getSymbols() )
            {
                if(!symbol->hasView())
                {
                    symbol->createView();
                    CSymbolView *symbolView = symbol->getSymbolView();
                    symbolView->initialize();
                    symbolView->setBarView(this);
                }

                symbol->getSymbolView()->setPos ( currentLeft,CGlobals::getTrackTopPadding());
                currentLeft += symbol->getSymbolView()->rect().width();
            }

            refreshLayout();
        }

        void CBarView::refreshLayout()
        {
            mBarSeparatorLine->setPos(rect().width(),CGlobals::getFirstMainLineTop());
        }

        //Protected methods
        int CBarView::calculateBarWidth()
        {
            int result = CGlobals::getBarLeftPadding() + ((CGlobals::getUnitSymbolWidth() * NOTE_VALUE_WHOLE * mModel->getParentScore()->getTime().getNumerator() ) / mModel->getParentScore()->getTime().getDenominator() ) + CGlobals::getBarRightPadding();
            return result;
        }

    }//namespace core
}//namespace easyscore
